/**
 * 
 */
package gwtool.gui.dataSetView;

import gwtool.container.DataSetItem;
import gwtool.container.OnlinePruefungItem;
import gwtool.gui.components.FormattedTextFieldWithMenu;
import gwtool.gui.components.JTextAreaWithMenu;
import gwtool.gui.components.JTextFieldWithMenu;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

/**
 * @author Christian
 * 
 */
public class DataSetPruefungPanel extends JPanel implements DataSetView {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6102798811238053532L;

	private DataSetViewController dataSetViewController;

	private JTextFieldWithMenu bezeichnerTextField;

	private JButton btnSchlieen;

	private JButton btnSpeichernButton;

	private JButton btnKopieErstellen;

	private FormattedTextFieldWithMenu formattedIntervallTextField;

	private JTextAreaWithMenu kommentarTextArea;

	private JButton btnDrucken;

	public DataSetPruefungPanel(DataSetViewController dataSetViewController, OnlinePruefungItem item, boolean zeigeButtons) {

		this.dataSetViewController = dataSetViewController;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);
		JLabel lblName = new JLabel("Name:");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 0;
		add(lblName, gbc_lblName);
		bezeichnerTextField = new JTextFieldWithMenu();
		GridBagConstraints gbc_nameTextField = new GridBagConstraints();
		gbc_nameTextField.insets = new Insets(0, 0, 5, 5);
		gbc_nameTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_nameTextField.gridx = 1;
		gbc_nameTextField.gridy = 0;
		add(bezeichnerTextField, gbc_nameTextField);
		bezeichnerTextField.setColumns(10);
		JLabel lblIntervall = new JLabel("Intervall:");
		GridBagConstraints gbc_lblIntervall = new GridBagConstraints();
		gbc_lblIntervall.anchor = GridBagConstraints.EAST;
		gbc_lblIntervall.insets = new Insets(0, 0, 5, 5);
		gbc_lblIntervall.gridx = 0;
		gbc_lblIntervall.gridy = 1;
		add(lblIntervall, gbc_lblIntervall);
		formattedIntervallTextField = new FormattedTextFieldWithMenu();
		GridBagConstraints gbc_formattedIntervallTextField = new GridBagConstraints();
		gbc_formattedIntervallTextField.insets = new Insets(0, 0, 5, 5);
		gbc_formattedIntervallTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_formattedIntervallTextField.gridx = 1;
		gbc_formattedIntervallTextField.gridy = 1;
		add(formattedIntervallTextField, gbc_formattedIntervallTextField);
		JLabel lblInMonaten = new JLabel("in Monaten");
		GridBagConstraints gbc_lblInMonaten = new GridBagConstraints();
		gbc_lblInMonaten.insets = new Insets(0, 0, 5, 0);
		gbc_lblInMonaten.gridx = 2;
		gbc_lblInMonaten.gridy = 1;
		add(lblInMonaten, gbc_lblInMonaten);
		JLabel lblKommentar = new JLabel("Kommentar:");
		GridBagConstraints gbc_lblKommentar = new GridBagConstraints();
		gbc_lblKommentar.insets = new Insets(0, 0, 5, 5);
		gbc_lblKommentar.gridx = 0;
		gbc_lblKommentar.gridy = 2;
		add(lblKommentar, gbc_lblKommentar);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 2;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		add(scrollPane, gbc_scrollPane);
		kommentarTextArea = new JTextAreaWithMenu();
		scrollPane.setViewportView(kommentarTextArea);
		kommentarTextArea.setLineWrap(true);
		kommentarTextArea.setText(item.getKommentar());
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 0, 5);
		gbc_panel.gridwidth = 2;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 3;
		add(panel, gbc_panel);
		panel.add(getBtnSpeichern());
		panel.add(getBtnKopieErstellen());
		panel.add(getBtnSchlieen());
		panel.add(getBtnDrucken());
		formattedIntervallTextField.setText(item.getInterval() + "");
		bezeichnerTextField.setText(item.getBezeichner());
		boolean erlaubt = dataSetViewController.isAendernErlaubt();
		if (!erlaubt) {
			kommentarTextArea.setEditable(false);
			bezeichnerTextField.setEditable(false);
			formattedIntervallTextField.setEditable(false);
			getBtnKopieErstellen().setEnabled(false);
			getBtnSpeichern().setEnabled(false);
		}
		if (!zeigeButtons) {
			getBtnKopieErstellen().setVisible(false);
			getBtnSpeichern().setVisible(false);
			getBtnSchlieen().setVisible(false);
		}
	}

	/**
	 * 
	 * @author Christian
	 * @return
	 * @see gwtool.gui.dataSetView.DataSetView#hasChanges()
	 */
	@Override
	public boolean hasChanges() {

		if (bezeichnerTextField.hasChanged()) {
			return true;
		}
		if (kommentarTextArea.hasChanged()) {
			return true;
		}
		if (formattedIntervallTextField.hasChanged()) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @author Christian
	 * @return
	 * @see gwtool.gui.dataSetView.DataSetView#getDataFromGui()
	 */
	@Override
	public DataSetItem getDataFromGui() {

		OnlinePruefungItem p = new OnlinePruefungItem();
		int i = 0;
		try {
			i = Integer.parseInt(formattedIntervallTextField.getText());
		} catch (Exception e) {
			i = 0;
		}
		p.setInterval(i);
		p.setKommentar(kommentarTextArea.getText());
		p.setBezeichner(bezeichnerTextField.getText());
		return p;
	}

	/**
	 * 
	 * @author Christian
	 * @return
	 * @see gwtool.gui.dataSetView.DataSetView#getJPanel()
	 */
	@Override
	public JPanel getJPanel() {

		return this;
	}

	/**
	 * 
	 * @author Christian
	 * @return
	 * @see gwtool.gui.dataSetView.DataSetView#getBtnSpeichern()
	 */
	@Override
	public JButton getBtnSpeichern() {

		if (btnSpeichernButton == null) {
			btnSpeichernButton = new JButton("speichern");
			btnSpeichernButton.addActionListener(dataSetViewController);
		}
		return btnSpeichernButton;
	}

	/**
	 * 
	 * @author Christian
	 * @return
	 * @see gwtool.gui.dataSetView.DataSetView#getBtnSchlieen()
	 */
	@Override
	public JButton getBtnSchlieen() {

		if (btnSchlieen == null) {
			btnSchlieen = new JButton("schlie\u00DFen");
			btnSchlieen.addActionListener(dataSetViewController);
		}
		return btnSchlieen;
	}

	/**
	 * 
	 * @author Christian
	 * @return
	 * @see gwtool.gui.dataSetView.DataSetView#getBtnKopieErstellen()
	 */
	@Override
	public JButton getBtnKopieErstellen() {

		if (btnKopieErstellen == null) {
			btnKopieErstellen = new JButton("Kopie erstellen");
			btnKopieErstellen.addActionListener(dataSetViewController);
		}
		return btnKopieErstellen;
	}

	@Override
	public void wurdeGespeichert() {

		bezeichnerTextField.setChangedFalse();
		kommentarTextArea.setChangedFalse();
		formattedIntervallTextField.setChangedFalse();
	}

	@Override
	public String getTabBezeichner() {

		return bezeichnerTextField.getText();
	}

	@Override
	public JButton getBtnDrucken() {

		if (btnDrucken == null) {
			btnDrucken = new JButton("Drucken");
			btnDrucken.addActionListener(dataSetViewController);
		}
		return btnDrucken;
	}
}
